package org.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<TestEnum> result = new ArrayList<>();
        test(TestEnum.A.val + TestEnum.H.val+ TestEnum.E.val + TestEnum.C.val, result);
        System.out.println(result);
    }

    public static void test(int val, List<TestEnum> result) {
        TestEnum[] enums = TestEnum.values();
        for (int i = enums.length - 1; i >= 0; i--) {
            while (val >= enums[i].val) {
                val -= enums[i].val;
                result.add(enums[i]);
            }
        }
        if (val != 0) {
            throw new IllegalArgumentException("The value cannot be decomposed into enum values.");
        }
        Collections.reverse(result);
    }
}